package com.huawei.spare.part.replace.utils;


import com.huawei.spare.part.replace.App;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import static com.huawei.spare.part.replace.Constants.*;

public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static String APP_DATA_FOLDER;

    public static File getProject(String projectId) {
        final String path = getProject() + File.separator + projectId;
        final File file = new File(path);
        if (!file.exists()) file.mkdirs();
        return file;
    }

    public static String getProject() {
        return getAppDataFolder() + File.separator + FolderProject;
    }

    /**
     * get config file path
     *
     * @return
     */
    public static InputStream getConfigFileInputStream() {
        return Application.class.getResourceAsStream("/configs.json");
//        return Application.class.getResource("/configs.json").toExternalForm();
    }

    public static String getTempFolder() {
        return getAppDataFolder() + File.separator + FolderTemp;
    }

    public static File getTempFile(String filename) {
        final String tempFolder = getTempFolder();
        final File folder = new File(tempFolder);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return new File(tempFolder + File.separator + filename);
    }

    public static String getDbTableFile(String table) {
        return getDatabaseFolder() + File.separator + table;
    }

    public static String getDbMetaFile() {
        return getDatabaseFolder() + File.separator + DbFileMeta;
    }

    public static String getDatabaseFolder() {
        return getAppDataFolder() + File.separator + FolderDB;
    }

    /**
     * get current APP data folder
     *
     * @return
     */
    public static String getAppDataFolder() {
        if (APP_DATA_FOLDER == null) {
            try {
                final URL source = App.class.getClass().getProtectionDomain().getCodeSource().getLocation();
                APP_DATA_FOLDER = new File(source.toURI()).getPath() + File.separator + "data";
            } catch (Exception e) {
                log.info("Could not locate application data folder");
                APP_DATA_FOLDER = new File("./data").getAbsolutePath();
                log.info("Use {} as application data folder", APP_DATA_FOLDER);
            }
        }
        return APP_DATA_FOLDER;
    }

    @Deprecated
    public static String getAppDataFolder2() {
        return getOSAppDataFolder() + File.separator + ApplicationName;
    }

    public static String getOSAppDataFolder() {
        String path = System.getenv("LOCALAPPDATA");
        if (new File(path).exists()) {
            return path;
        }

        path = System.getenv("APPDATA");
        if (new File(path).exists()) {
            return path;
        }

        throw new RuntimeException("Could not get app data folder");
    }

}
